package com.eugenema.util.ExcelUtil;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.eugenema.entity.EvenMatch;
import org.apache.poi.ss.usermodel.IndexedColors;

/**
 * “连场”字段转换器
 * 将读取到的数据转成实体类
 *
 * @author Eugenema
 * @date 2023/3/5 17:15
 */
public class EvenMatchConverter implements Converter<EvenMatch> {
    /**
     * 返回 Excel 读取到的数据，对应 Java 中的属性类型
     *
     * @author Eugenema
     * @date 2023/3/5 17:18
     *
     *
     * @return 属性类型
     **/
    @Override
    public Class<?> supportJavaTypeKey() {
        return EvenMatch.class;
    }

    /**
     * 返回 Excel 读取到的数据类型
     *
     * @author Eugenema
     * @date 2023/3/5 17:18
     *
     *
     * @return Excel 内部数据类型
     **/
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 将文本封装进实体类 EvenMatch
     * 读数据时调用
     *
     * @author Eugenema
     * @date 2023/3/5 17:22
     *
     * @param context 读取转换器上下文对象
     *
     * @return 转换后的值
     **/
    @Override
    public EvenMatch convertToJavaData(ReadConverterContext<?> context) {
        String cellContext = context.getReadCellData().getStringValue();
        return EvenMatch.evenMatchConverter(cellContext);
    }

    /**
     * 将实体类转成字符串，且“单”为红，“双”为蓝
     * 写数据时调用
     *
     * @author Eugenema
     * @date 2023/3/5 17:28
     *
     * @param context 写转换器上下文对象
     *
     * @return 写入的值
     **/
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<EvenMatch> context) {
        WriteCellData<String> writeCellData = new WriteCellData<>(context.getValue().toString());
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        WriteFont writeFont = new WriteFont();
        writeFont.setColor(context.getValue().isDirection() ? IndexedColors.SKY_BLUE.getIndex() : IndexedColors.RED.getIndex());
        writeCellStyle.setWriteFont(writeFont);
        writeCellData.setWriteCellStyle(writeCellStyle);
        return writeCellData;
    }
}
